package br.com.eresinformatica.prototipo.dados;

import java.io.ByteArrayOutputStream;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Upload extends Activity {

	private MenuItem item;
	private String url = "http://10.0.2.2:8080/baixando_imagem/Imagem/get";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_upload);

		final EditText edtTxt1 = (EditText) findViewById(R.id.editTextUpl1);
		final EditText edtTxt2 = (EditText) findViewById(R.id.editText2);
		Button btnUpl = (Button) findViewById(R.id.upload);

		btnUpl.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				String param1 = edtTxt1.getText().toString();
				String param2 = edtTxt2.getText().toString();

				item.setActionView(R.layout.activity_progress);
				SendHttpRequestTask t = new SendHttpRequestTask();

				String[] params = new String[] { url, param1, param2 };
				t.execute(params);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.upload, menu);
		item = menu.getItem(0);
		return true;

	}

	private class SendHttpRequestTask extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... params) {
			String url = params[0];
			String param1 = params[1];
			String param2 = params[2];
			Bitmap b = BitmapFactory.decodeResource(Upload.this.getResources(), R.drawable.logo);

			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			b.compress(CompressFormat.PNG, 0, baos);

			try {
				HttpClient client = new HttpClient(url);
				client.connectForMultipart();
				client.addFormPart("param1", param1);
				client.addFormPart("param2", param2);
				client.addFilePart("file", "logo.png", baos.toByteArray());
				client.finishMultipart();
				String data = client.getResponse();
			} catch (Throwable t) {
				t.printStackTrace();
			}

			return null;
		}

		@Override
		protected void onPostExecute(String data) {
			item.setActionView(null);

		}

	}

}
